package huanzhao.erp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import huanzhao.erp.domain.entity.GlobalSetting;
import huanzhao.erp.domain.entity.Label;
import huanzhao.erp.domain.entity.Phone;
import huanzhao.erp.domain.po.Result;
import huanzhao.erp.domain.vo.GlobalSettingVO;
import huanzhao.erp.domain.vo.LabelVO;
import huanzhao.erp.domain.vo.PhoneVO;
import huanzhao.erp.domain.vo.TaskVO;
import huanzhao.erp.service.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author April
 * 2024/10/2 09:25
 * @version 1.0
 */
@RestController
@RequestMapping("api")
@Validated
public class DataController {
    private final BasicService basicService;
    private final PhoneService phoneService;
    private final TaskService taskService;
    private final StaffService staffService;
    private final MiscService miscService;

    public DataController(BasicService basicService, PhoneService phoneService, TaskService taskService, StaffService staffService, MiscService miscService) {
        this.basicService = basicService;
        this.phoneService = phoneService;
        this.taskService = taskService;
        this.staffService = staffService;
        this.miscService = miscService;
    }

    /**
     * 按条件查询手机
     *
     * @param state    手机状态
     * @param pageNum  当前页码
     * @param pageSize 每页记录数量
     * @return IPage<Phone>
     * 2024/6/11 下午9:07:59
     */
    @GetMapping("/phone")
    public Result<IPage<PhoneVO>> getPhones(@RequestParam(required = false) String state, @RequestParam(required = false) String trackingBegin, @RequestParam(value = "imei", required = false) String IMEI, @RequestParam int pageNum, @RequestParam int pageSize) {
        return Result.ok(phoneService.getPhonesByCondition(state, trackingBegin, IMEI, pageNum, pageSize));
    }

    /**
     * 批量查询手机
     *
     * @param ids 手机id列表
     * @return List<Phone>
     * 2024/7/21 下午6:01:05
     */
    @GetMapping("/phone/batch")
    public Result<List<PhoneVO>> getPhones(@RequestParam List<Long> ids) {
        if (ids == null || ids.isEmpty() || ids.size() > 30) {
            return Result.ok(Collections.emptyList());
        }
        return Result.ok(phoneService.getPhonesInBatch(ids));
    }

    /**
     * 获取手机流转记录
     *
     * @param phoneId 手机id
     * @return List<Task>
     * 2024/6/17 下午8:47:44
     */
    @GetMapping("/phone/logs/{phoneId}")
    public Result<List<TaskVO>> getPhoneLogs(@PathVariable Long phoneId) {
        Phone phone = phoneService.getById(phoneId);
        if (phone == null) {
            return Result.ok(Collections.emptyList());
        }
        return Result.ok(phoneService.getPhoneLogs(phoneId));
    }

    /**
     * 修改手机字段
     *
     * @param phoneId 手机id
     * @param attr    属性名
     * @param value   属性值
     * @return Result<?>
     * 2024/10/2 13:16:50
     */
    @PutMapping("/phone")
    public Result<?> updatePhoneAttr(@RequestParam long phoneId, @RequestParam String attr, @RequestParam String value) {
        return Result.ok();
    }

    /**
     * 手动添加手机成本
     *
     * @param phoneId      手机id
     * @param isRepairCost 是否为维修成本
     * @param cost         成本
     * @param comment      备注
     * @return Result<?>
     * 2024/10/2 13:17:13
     */
    @PostMapping("/phone/cost")
    public Result<?> addCost(@RequestParam long phoneId, @RequestParam boolean isRepairCost, @RequestParam double cost, @RequestParam(required = false) String comment) {
        return Result.ok();
    }

    /**
     * 按条件查询任务
     *
     * @param type     任务类型
     * @param state    任务状态
     * @param pageNum  当前页码
     * @param pageSize 每页记录数量
     * @return IPage<Task>
     * 2024/6/11 下午9:08:31
     */
    @GetMapping("/task")
    public Result<IPage<TaskVO>> getTasksByCondition(@RequestParam(required = false) String type, @RequestParam(required = false) Integer state, @RequestParam(required = false) Boolean all, @RequestParam(required = false) Long teamId, @RequestParam(required = false) Integer dept, @RequestParam int pageNum, @RequestParam int pageSize) {
        return Result.ok(taskService.getTasksByCondition(type, state, all, pageNum, pageSize));
    }

    /**
     * 查询全局设置
     *
     * @return Result<GlobalConfigVO>
     * 2024/10/2 13:17:49
     */
    @GetMapping("/setting")
    public Result<GlobalSettingVO> getGlobalSetting() {
        GlobalSettingVO globalSetting = miscService.getGlobalSetting();
        if (globalSetting == null) {
            return Result.error("全局设置不存在");
        }
        return Result.ok(globalSetting);
    }

    /**
     * 修改全局设置
     *
     * @return Result<?>
     * 2024/10/2 13:17:56
     */
    @PutMapping("/setting")
    public Result<?> updateGlobalSetting(@RequestBody GlobalSetting globalSetting) {
        Result<?> result = miscService.checkGlobalSetting(globalSetting);
        if (result.isError()) {
            return result;
        }
        return miscService.updateGlobalSetting(globalSetting);
    }

    /**
     * 查询所有标签
     *
     * @return Result<List < LabelVO>>
     * 2024/10/2 13:18:02
     */
    @GetMapping("label")
    public Result<List<LabelVO>> getAllLabels() {
        return Result.ok(miscService.getAllLabels());
    }

    /**
     * 按id查询标签
     *
     * @return Result<LabelVO>
     * 2024/10/2 13:18:05
     */
    @GetMapping("label/{labelId}")
    public Result<LabelVO> getLabelById(@PathVariable long labelId) {
        LabelVO labelVO = miscService.getLabelById(labelId);
        if (labelVO == null) {
            return Result.error("标签不存在");
        }
        return Result.ok(labelVO);
    }

    /**
     * 添加标签
     *
     * @return Result<?>
     * 2024/10/2 13:18:18
     */
    @PostMapping("label")
    @SaCheckPermission("company:label:add")
    public Result<?> addLabel(@RequestBody Label label) {
        return miscService.addLabel(label);
    }

    /**
     * 修改标签
     *
     * @return Result<?>
     * 2024/10/2 13:18:21
     */
    @PutMapping("label")
    @SaCheckPermission("company:label:edit")
    public Result<?> updateLabel(@RequestBody Label label) {
        return miscService.updateLabel(label);
    }

    /**
     * 删除标签
     *
     * @return Result<?>
     * 2024/10/2 13:18:25
     */
    @DeleteMapping("label/{labelId}")
    @SaCheckPermission("company:label:remove")
    public Result<?> deleteLabelById(@PathVariable long labelId) {
        return miscService.deleteLabelById(labelId);
    }
}
